package com.systop.party05.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.systop.party05.R;
import com.systop.party05.base.BaseActivity;
import com.systop.party05.entity.ComEntity;
import com.systop.party05.http.ComUrl;
import com.systop.party05.http.MyStringCallback;
import com.systop.party05.utils.CountDownTimerUtils;
import com.systop.party05.utils.GeneralTools;
import com.systop.party05.utils.MD5Utils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.systop.party05.http.ComUrl.REGISTER;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.register_edt_phone)
    EditText registerEdtPhone;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.register_edt_code)
    EditText registerEdtCode;
    @BindView(R.id.register_tv_get_code)
    TextView registerTvGetCode;
    @BindView(R.id.register_edt_idcard)
    EditText registerEdtIdcard;
    @BindView(R.id.register_edt_name)
    EditText registerEdtName;
    @BindView(R.id.register_edt_psw)
    EditText registerEdtPsw;
    @BindView(R.id.register_tv_register)
    TextView registerTvRegister;

    private   CountDownTimerUtils countDownTimerUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_register);
        setIvBack();
        setTvTitle("注册");
        ButterKnife.bind(this);
    }

    @OnClick({R.id.register_tv_get_code, R.id.register_tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register_tv_get_code:
                //获取验证码
                String phone = registerEdtPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    showMessage(registerEdtPhone.getHint().toString());
                } else if (phone.length() != 11) {
                    showMessage(getString(R.string.phone_error));
                } else {
                    if(countDownTimerUtils==null) {
                        countDownTimerUtils = new CountDownTimerUtils(registerTvGetCode, 60000, 1000);
                    }
                    if(countDownTimerUtils.getTimerStatu()==0||countDownTimerUtils.getTimerStatu()==2){
                        countDownTimerUtils.start();
                        okhttpSendCode();//调用验证码接口
                    }else if(countDownTimerUtils.getTimerStatu()==1){
                        showMessage(getString(R.string.getCode_wait));
                    }
                }
                break;
            case R.id.register_tv_register:
                if(!isEmply()){
                    okhttpRegister();
                }
                break;
        }
    }

    private void okhttpSendCode() {
        HashMap<String, String> param = new HashMap<>();
        param.put("phone", registerEdtPhone.getText().toString().trim());//手机号
        commonOkhttp.execute(ComUrl.SEND_CODE, param, new MyStringCallback(this) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                ComEntity entity = gson.fromJson(response, ComEntity.class);
                showMessage(entity.getMsg());
            }
        });
    }

    private void okhttpRegister() {
        HashMap<String, String> param = new HashMap<>();
        param.put("client", GeneralTools.getDeviceId(this));//设备id
        param.put("phone", registerEdtPhone.getText().toString().trim());//手机号
        param.put("code", registerEdtCode.getText().toString().trim());//验证码
        param.put("sfzh", registerEdtIdcard.getText().toString().trim());//身份证
        param.put("name", registerEdtName.getText().toString().trim());//姓名
        param.put("password", MD5Utils.MD5(registerEdtPsw.getText().toString().trim()));//密码
        commonOkhttp.execute8(REGISTER, param, new MyStringCallback(this) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                ComEntity entity = gson.fromJson(response, ComEntity.class);
                showMessage(entity.getMsg());
                finishActivity();
            }
        });
    }


    private boolean isEmply() {
        String phone = registerEdtPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showMessage(registerEdtPhone.getHint().toString());
            return true;
        }
        if (phone.length() != 11) {
            showMessage(getString(R.string.phone_error));
            return true;
        }
        if (TextUtils.isEmpty(registerEdtCode.getText())) {
            showMessage(registerEdtCode.getHint().toString());
            return true;
        }
        String idcard = registerEdtIdcard.getText().toString().trim();
        if (TextUtils.isEmpty(registerEdtIdcard.getText())) {
            showMessage(registerEdtIdcard.getHint().toString());
            return true;
        }
        if (idcard.length() != 18) {
            showMessage(getString(R.string.idcard_error));
            return true;
        }
        if (TextUtils.isEmpty(registerEdtName.getText())) {
            showMessage(registerEdtName.getHint().toString());
            return true;
        }
        String psw = registerEdtPsw.getText().toString().trim();
        if (TextUtils.isEmpty(psw)) {
            showMessage(registerEdtPsw.getHint().toString());
            return true;
        }
        if (psw.length() < 8) {
            showMessage(getString(R.string.psw_error));
            return true;
        }
        return false;
    }
}